<?php
namespace Home\Controller;

Header('Content-Type: text/html; charset=utf-8');
use Sms\Sender;
use Org\Util\Qqconnect;
use LT\ThinkSDK\ThinkOauth;
use Think\Page;

class UserController extends CommonController {
    public function index(){
        if (is_login()) {
            $this->redirect('user/login');
        }
    }
    public function login(){
        $this->display();
    }

    public function code(){
        $Verify = new \Think\Verify();
        $Verify->fontSize = 30;
        $Verify->length   = 3;
        $Verify->useNoise = false;
        $Verify->entry();
    }


    public function login_c(){
        $users=D('user');
        $data['user']=I('param.user');
        $data['pwd']=I('param.pwd');
        $data['code']=I('param.code');
        if(check_verify(I('param.code'))) {
            $user = null;
            if (strpos($data['user'], '@') == true) {
                $map['email'] = I('param.user');
                $user = $users->where($map)->find();//用邮箱查
            } else {
                $map['tel'] = I('param.user');
                $user = $users->where($map)->find();//用电话号码查
            }

            $m3_result = array();
            if ($user == null) {
                $m3_result['status'] = 1;
                $m3_result['message'] = '用户名不存在';
                $this->ajaxReturn($m3_result);
            } else {
                if (md5('bk'.$data['pwd']) != $user['passowrd']) {
                    $m3_result['status'] = 2;
                    $m3_result['message'] = '密码不正确';
                    $this->ajaxReturn($m3_result);
                }
            }
            session_start();
            session('user',$user);
            $m3_result['status'] = 0;
            $m3_result['message'] = '恭喜你登陆成功';
            $this->ajaxReturn($m3_result);
        }else{
            $m3_result['status'] = 3;
            $m3_result['message'] = '验证码错误';
            $this->ajaxReturn($m3_result);
        }
    }

    public function register(){
        $this->display();
    }

    public function register_c(){
        if(IS_POST){
            $m3_result = array();
            $user=D('user');//实例化用户表
            $telcode=D('telcode');//实例化电话短信表
            $map['tel']=I('param.phone');//将电话的值赋值
            $code=$telcode->where($map)->find();
            if($code['code'] == I('param.code')) {
                $data['username']=I('param.user');
                $data['passowrd']=md5('bk'.I('param.pwd'));
                $data['tel']=I('param.phone');
                $data['email']=I('param.email');
                $data['ctime']=time();
                $res=$user->data($data)->add();
                $m3_result['status'] = 0;
                $m3_result['message'] = '注册成功';
                $this->ajaxReturn($m3_result);
            }else{
                $code['code']=I('param.code');
                $m3_result['status'] = 1;
                $m3_result['message'] = '您的验证码错误';
                $this->ajaxReturn($m3_result);
            }
        }
    }

    public function  login_san(){
        $qqobj=new Qqconnect('101410495','ac05e6043b21d03b58c5124cbf8c417e','0');
        $qqobj->getAuthCode();
    }

    public function login_weixin(){
        $AppID = 'wx411c84fdc40c4f8e';
        $AppSecret = 'ff72dfc3b4315db8b923e85c9342a63a';
        $callback  =  'http://www.eccms.net/personal';
        session_start();
        $state  = md5(uniqid(rand(), TRUE));//生成随机串防CSRF攻击
        $_SESSION["wx_state"]    =   $state;
        $wxurl = "https://open.weixin.qq.com/connect/qrconnect?appid=".$AppID."&redirect_uri=http://www.eccms.net/personal&response_type=code&scope=snsapi_login&state={$state}#wechat_redirect";
        //$wxurl ="https://open.weixin.qq.com/connect/qrconnect?appid=".$AppID."&redirect_uri=http://www.eccms.net/personal&response_type=code&scope=SCOPE&state={$state}#wechat_redirect";
        header("Location: $wxurl");
    }




    public function send_code(){
        $m3_result = array();
        $telcode=D('telcode');

        $map['tel']=I('param.phone');
        $code=$telcode->where($map)->find();
        if($code==null){
            $config = D('sms_config');
            $phone = I('phone');
            $sms = $config->where('id=1')->find();
            $send = new Sender($sms['appid'], $sms['appkey']);
            $code=mdrand();
            $params = array($code);
            $res = $send->send($phone, '19271', $params);
            $telcode=D('telcode');
            $data['tel']=$phone;
            $data['code']=$code;
            $res1=$telcode->data($data)->add();
            $m3_result['status'] = 0;
            $m3_result['message'] = '短信发送成功';
            $this->ajaxReturn($m3_result);
        }else{
            $m3_result['status'] = 1;
            $m3_result['message'] = '您的手机号已经注册';
            $this->ajaxReturn($m3_result);
        }

    }

    public function retrieve(){
        $this->display();
    }
    public function send_find(){//短信接口
        $m3_result = array();//定义返回数组
        $telcode=D('telcode');//实例化短信验证码表
        $map['tel']=I('param.phone');//将页面传输过来的电话号码赋值给$map['tel']
        $code=$telcode->where($map)->find();//查询短信验证码表
        if($code==null){//判断查询到的值是否为空
            $config = D('sms_config');//实例化短信接口表
            $phone = I('phone');//将传过来的电话赋值
            $sms = $config->where('id=1')->find();//查询短信接口所需的数据
            $send = new Sender($sms['appid'], $sms['appkey']);//调用短信接口方法
            $code=mdrand();//定义一个空集赋值
            $params = array($code);//定义数组接受空集
            $res = $send->send($phone, '19275', $params);//短信接口
            $telcode=D('telcode');
            $data['tel']=$phone;
            $data['code']=$code;
            $res1=$telcode->data($data)->add();
            $m3_result['status'] = 0;
            $m3_result['message'] = '短信发送成功';
            $this->ajaxReturn($m3_result);
        }else{
            $m3_result['status'] = 1;
            $m3_result['message'] = '发送失败';
            $this->ajaxReturn($m3_result);
        }

    }

    public function send_zz(){
        $data=I('post.');
        $phone=$data['phone'];
        $code=$data['code'];
        $telcode = D('telcode');
        $is= $telcode->where(array('tel'=>$phone))->find();
        cookie('phone',$phone);
        if ($is['code']==$code){
            $this->ajaxReturn(0);
        }else{
            $this->ajaxReturn('验证码错误');
        }
    }

    public function send_xxx(){
        $m3_result = array();
        $phon=I('post.');
        $phone=$phon['phone'];
    if(!$phone==null){
            if($phon['leng']=='11'){
                $telcode = D('telcode');
                $user= D('user');
                $is= $telcode->where(array('tel'=>$phone))->find();
                $usere=$user->where(array('tel'=>$phone))->find();
                if($is==null && $usere==null){
                    $m3_result=array("手机号码未注册");
                }else{
                    $telcode->where(array('tel'=>$phone))->delete();
                }
            }else{
                $m3_result=array("手机号码格式不正确");
            }
        }
     $this->ajaxReturn($m3_result);

    }

    public function member(){

        if(!session('user')){
            $this->redirect('user/login');
        }
        $user=session('user');
        $bill=M('bill')->sum('value');
        $bugs=M('bug')->count();
        $project=M('project')->count();
        $map['status'] = 1;
        $articlelist = M('info')->where($map)->order('ctime desc')->limit(5)->select();
        $users = M('user')->where($map)->order('money desc')->limit(4)->select();
        $this->assign('project',$project);
        $this->assign('articlelist',$articlelist);
        $this->assign('bugs',$bugs);
        $this->assign('bill',$bill);
        $this->assign('user',$user);
        $this->assign('users',$users);
        $this->display();
    }

    public function tuichu(){
        $user=session('user');
        if ($user){
            session_destroy($user);
            $this->redirect('Index/index');
        }

    }


    public function find(){
        $this->display();
    }
    public function send_aa(){
        $passowrd=I('post.passowrd');

        /*dump($passowrd);die;*/
        $phone= $_COOKIE['phone'];
        $m3_result = array();
        $user=D('user');
        $a=$user-> where(array('tel'=>$phone))->save(array('passowrd'=>md5('bk'.$passowrd)));
        if ($a){
            $m3_result['status'] = 0;
            $m3_result['message'] = '修改成功';
            $this->ajaxReturn($m3_result);
        }else{
            $m3_result['status'] = 1;
            $m3_result['message'] = '修改失败';
            $this->ajaxReturn($m3_result);
        }
    }

    public function project(){//$type=0
        session_start();
        if(!session('user')){
            $this->redirect('user/login');
        }
        $user=session('user');
        $this->assign('user',$user);
        $map = array('status'=>array('neq', '0'));//要求状态不等于0且漏洞类型等于0  ,'type_id'=>$type
        $projects = M('project')->where($map)->select();
        //dump($projects);die;
        $this->assign('projects',$projects);
        $this->display();
    }

    public function myproject(){
        session_start();
        if(!session('user')){
            $this->redirect('user/login');
        }
        $user=session('user');
        $this->assign('user',$user);
        $map['user_id']=$user['id'];//将id赋值
        $company = M('company')->where($map)->find();//根据id查询厂商
        $con['company_id']=$company['id'];//将厂商id赋值给项目id
        $projects = M('project')->where($con)->select();//通过项目id查询项目
        //dump($company);die;
        $this->assign('projects',$projects);
        $this->display();
    }

    public function mybugs(){
        session_start();
        if(!session('user')){
            $this->redirect('user/login');
        }
        $user=session('user');
        $data=I('post.search_name');
        $this->assign('user',$user);
        $title['wc_bug.title']=array('like','%'.$data.'%');
        $usere['title']=array('user_id',$user['id']);
        $field='wc_bug.id,wc_bug.title,wc_company.name,wc_bug.ctime,wc_bug.status,wc_bug.level,wc_project.title as zz ';
        if ($data==""){
            $bug=M('bug')->field($field)->join('wc_project ON wc_bug.project_id = wc_project.id')->join('wc_company ON wc_bug.company_id = wc_company.id')
                ->join('wc_bug_type ON wc_bug.type_id = wc_bug_type.id') ->select();
            $this->assign('bug',$bug);
        }else{
            $bugz=M('bug')->field($field)->join('wc_project ON wc_bug.project_id = wc_project.id')->join('wc_company ON wc_bug.company_id = wc_company.id')
                ->join('wc_bug_type ON wc_bug.type_id = wc_bug_type.id') ->where($title,$usere)->select();
            $this->assign('bug',$bugz);
        }
        $this->display();
    }

    public function subbug(){
        session_start();
        if(!session('user')){
            $this->redirect('user/login');
        }
        $user=session('user');
        //$company=D('company')->where(array('user_id'=>$user['id']))->find();
        $company=D('company')->select();
        $project=D('project')->select();
        //dump($company);die;
        $this->assign('chanshan',$company);//将厂商信息引用到页面中
        $this->assign('project',$project);
        $this->assign('user',$user);
        $this->display();
    }

    public function subbug_ldTJ(){
        $date=I('post.');
        //dump($date);die;
        $user=session('user');
        $bug=M('bug');
        $data=array();
        $data['user_id']=$user['id'];//user_id
        $data['company_id']=$date['bug_firm_name'];//厂商id
        $data['project_id']=$date['title'];//项目id
        $data['type_id']=$date['sectionid'];//漏洞类型id
        $data['title']=$date['bug_title'];//漏洞标题
        $data['desc']=$date['bug_description'];//漏洞描述
        $data['level']=$date['bug_level'];//危险等级
        $data['detail']=$date['detail'];//漏洞详情
        $data['repair']=$date['repair'];//漏洞解决方案
        $data['ctime']=time();
/*        $auto = array(
            array('ctime', 'time', 1, 'function')
        );
        $bug->auto($auto)->create();*/
        $zz=$bug->add($data);

        if ($zz){
            $this->ajaxReturn('nice');
        }else{
            $this->ajaxReturn('fuck');
        }
    }


    public function myfav(){
        session_start();
        if(!session('user')){
            $this->redirect('user/login');
        }
        $user=session('user');
        $this->assign('user',$user);
        $this->display();
    }

    public function myteam(){
        session_start();
        if(!session('user')){
            $this->redirect('user/login');
        }
        $user=session('user');
        $name=I('post.search');
        $this->assign('user',$user);
        if ($name){
            $title['name']=array('like','%'.$name.'%');
            $usere['status']=array('status',1);
            $totalRows = M('team')->count();//==2
            $page=new Page($totalRows, 2);
            $page->setConfig('prev', '上一页');
            $page->setConfig('next', '下一页');
            $this->team = M('team')->limit($page->firstRow, $page->listRows)->where($title,$usere)->select();
            $this->assign('page', $page);
        }else{
            $totalRows = M('team')->count();//==2
            $page=new Page($totalRows, 2);
            $page->setConfig('prev', '上一页');
            $page->setConfig('next', '下一页');
            $this->team = M('team')->limit($page->firstRow, $page->listRows)->select();
            $this->assign('page', $page);
        }

        /*$title['name']=array('like','%'.$name['name'].'%');
        $usere['status']=array('status',1);
        $team = M('team')->where($title,$usere)->select();*/

        //$this->ajaxReturn($team);*/
        $this->display();
    }

    public function myintro(){
        //session_start();
        if(!session('user')){
            $this->redirect('user/login');
        }
        $user=session('user');
        //dump($user);die;
        $users=D('user')->where(array('id'=>$user['id']))->find();
        $this->assign('user',$users);
        $this->display();
    }

    public function myintro_xiugai(){
        $data=I('post.');
        //$this->ajaxReturn($data);
        $tel=$data['name'];
        $m3_result = array();
        $user=D('user');
        $date=array(
            'avatar'=>$data['img'],
            'country'=>$data['country'],
            'weibo'=>$data['weibo'],
            'github'=>$data['github'],
            'sign'=>$data['about']
        );
        $jieguo=$user->where(array('tel'=>$tel))->save($date);
        if ($jieguo){
            $m3_result['status'] = 0;
            $m3_result['message'] = '修改成功';
            $this->ajaxReturn($m3_result);
        }else{
            $m3_result['status'] = 1;
            $m3_result['message'] = '修改失败';
            $this->ajaxReturn($m3_result);
        }
    }

    public function firm(){
        session_start();
        if(!session('user')){
            $this->redirect('user/login');
        }
        $user=session('user');
        //$company=D('com');
        //dump($user);
        $this->assign('user',$user);
        $this->display();
    }
    public function firm_find(){
        $m3_result = array();//定义返回数组
        $telcode=D('telcode');//实例化短信验证码表
        $map['tel']=I('param.phone');//将页面传输过来的电话号码赋值给$map['tel']
        $zz=$telcode->where(array('tel'=>$map['tel']))->delete();
        //dump(M($zz)->_sql());die;
        $code=$telcode->where($map)->find();//查询短信验证码表
        if($code==null){//判断查询到的值是否为空
            $config = D('sms_config');//实例化短信接口表
            $phone = I('phone');//将传过来的电话赋值
            $sms = $config->where('id=1')->find();//查询短信接口所需的数据
            $send = new Sender($sms['appid'], $sms['appkey']);//调用短信接口方法
            $code=mdrand();//定义一个空集赋值
            $params = array($code);//定义数组接受空集
            $res = $send->send($phone, '19275', $params);//短信接口
            $telcode=D('telcode');
            $data['tel']=$phone;
            $data['code']=$code;
            $res1=$telcode->data($data)->add();
            $m3_result['status'] = 0;
            $m3_result['message'] = '短信发送成功';
            $this->ajaxReturn($m3_result);
        }else{
            $m3_result['status'] = 1;
            $m3_result['message'] = '发送失败';
            $this->ajaxReturn($m3_result);
        }

    }

    public function firm_add(){
        $m3_result = array();
        $data=I('post.');
       //$this->ajaxReturn($data);
/*        $this->ajaxReturn($data);
        //$name=$data['name'];//厂商
        $code=$data['code'];//验证码
        //$contacts=$data['linkman'];//联系人
        //$department=$data['section'];//部门
        //$email=$data['email'];//邮箱
        //$hpmepage=$data['url'];//主页
        $tel=$data['tel'];//手机号
        //$trade=$data['trade'];//行业
        //$domain=$data['domain'];//域名*/
        $telcode=D('telcode');
        $cods=$telcode->where(array('tel'=>$data['tel']))->find();
        $codes=$cods['code'];
        if ($data['code'] == $codes){
            $rules = array(
                array('name', 'require', '厂商名称必须填写'), // 默认情况下用正则进行验证user_
                array('user_id', 'require', 'id不为空'), // 默认情况下用正则进行验证
                array('url', 'require', '主页必须填写'),
                array('domain', 'require', '域名必须填写'),
                array('trade', 'require', '行业必须填写'),
                array('linkman', 'require', '联系人必须填写'),
                array('section', 'require', '部门必须填写'),
                array('email', 'require', '邮箱必须是企业邮箱'),
            );
            $auto = array(
                array('ctime', 'time', 1, 'function')
            );
            $User = M("company"); // 实例化User对象
            if (! $User->validate($rules)->create()) { // 验证没通过
                $this->error($User->getError());
            } else {
                $User->auto($auto)->create();
                $res = $User->add();
                if ($res) {
                    $m3_result['status'] = 3;
                    $m3_result['info'] = '提交成功';
                    $this->ajaxReturn($m3_result);
                } else {
                    $this->error('添加失败');
                }
            }
        }else{
            $m3_result['status'] = 0;
            $m3_result['info'] = '验证码错误';
            $this->ajaxReturn($m3_result);
        }


    }

    public function crash(){
        session_start();
        if(!session('user')){
            $this->redirect('user/login');
        }
        $user=session('user');
        $this->assign('user',$user);
        $this->display();
    }

    public function bindphone(){
        session_start();
        if(!session('user')){
            $this->redirect('user/login');
        }
        $user=session('user');
        $this->assign('user',$user);
        $this->display();
    }

    public function weixin(){
        session_start();
        if(!session('user')){
            $this->redirect('user/login');
        }
        $user=session('user');
        $this->assign('user',$user);
        $this->display();
    }

    public function amend(){
        session_start();
        if(!session('user')){
            $this->redirect('user/login');
        }
        $user=session('user');
        $this->assign('user',$user);
        $this->display();
    }

/*    public function xiugaimima(){
        $password=I('post.password');
        $phone=session('user.tel');
        $users=D('user');
        $data=$users->where(array('tel'=>$phone))->field('passowrd')->find();
        $zz=$data['passowrd'];
        //$this->ajaxReturn($zz);
        if (md5('bk'.$password) == $zz){
            $this->ajaxReturn(0);
        }else{
            $this->ajaxReturn(1);
        }

    }*/
    public function amend_xgmm(){
        $m3_result = array();
        $data=I('post.');
        $pwd=$data['pwd'];
        $newpwd=$data['newpwd'];
        $pwds=$data['pwds'];
        $phone=session('user.tel');
        $users=D('user');
        $user=$users->where(array('tel'=>$phone))->field('passowrd')->find();
        $ymm=$user['passowrd'];
        $zz=strlen($newpwd);
        //dump($zz);die;
        if (md5('bk'.$pwd) == $ymm){//判断其是否与数据库原密码相同
            if ($newpwd != null && $zz>=6){//判断新密码是否符合规范
                if ($pwds == $newpwd ){//判断确认密码与新密码是否一致
                    $a=$users-> where(array('tel'=>$phone))->save(array('passowrd'=>md5('bk'.$pwds)));
                    //echo M()->_sql($a);die;
                    if ($a){
                        $this->ajaxReturn(0);
                    }else{
                        $this->ajaxReturn(4);
                    }
                }else{
                    $this->ajaxReturn(3);
                }
            }else{
                $this->ajaxReturn(2);
            }
        }else{
            $this->ajaxReturn(1);
        }

    }

    public function upload(){
        define('WEB_ROOT', str_replace("\\", '/', dirname(dirname(__FILE__))));

        foreach($_FILES as $k=>$v){
            $fs=$_FILES[$k];
            break;
        }
        if($fs){
            $torrent = explode(".", $fs["name"]);
            $fileend = end($torrent);
            $fileend = strtolower($fileend);

            $path = '/Home/uploades/'.time() . rand(1, 999999) . '.' . $fileend;
            $rs = move_uploaded_file($fs["tmp_name"], WEB_ROOT . $path);

            if ($rs) {
                echo json_encode(array('result'=>true,'path'=>$_SERVER['REQUEST_SCHEME'] . $path));
            }else{
                echo json_encode(array('result'=>false,'errormsg'=>'系统错误'));
            }
        }else{
            echo json_encode(array('result'=>false,'errormsg'=>'没有上传的文件'));
        }
    }
}
